package com.example.dw.admin.controller;

import com.example.dw.common.Result;
import com.example.dw.admin.dto.PageRequestDTO;
import com.example.dw.admin.dto.CreateNewsDTO;
import com.example.dw.admin.dto.UpdateNewsDTO;
import com.example.dw.admin.service.AdminNewsService;
import com.example.dw.entity.News;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 管理端新闻管理控制器
 * @author system
 * @since 2024-01-01
 */
@Tag(name = "管理端新闻管理", description = "新闻信息管理接口")
@Slf4j
@RestController
@RequestMapping("/admin")
@Validated
public class AdminNewsController {

    @Autowired
    private AdminNewsService adminNewsService;

    /**
     * 获取部分新闻
     */
    @Operation(summary = "获取部分新闻", description = "获取指定数量的新闻")
    @GetMapping("/newsSome/{count}")
    public Result<?> getSomeNews(@PathVariable Integer count) {
        try {
            List<News> news = adminNewsService.getSomeNews(count);
            return Result.success("获取新闻列表成功", news);
        } catch (Exception e) {
            log.error("获取部分新闻失败: {}", e.getMessage());
            return Result.error("获取新闻列表失败");
        }
    }

    /**
     * 分页获取新闻信息
     */
    @Operation(summary = "分页获取新闻信息", description = "带其他参数分页查询新闻信息")
    @PostMapping("/newsPage")
    public Result<?> getNewsByPage(@RequestBody @Validated PageRequestDTO pageRequest) {
        try {
            Map<String, Object> result = adminNewsService.getNewsByPage(pageRequest);
            return Result.success("获取新闻列表成功", result);
        } catch (Exception e) {
            log.error("分页获取新闻信息失败: {}", e.getMessage());
            return Result.error("获取新闻列表失败");
        }
    }

    /**
     * 获取新闻信息
     */
    @Operation(summary = "获取新闻信息", description = "根据新闻ID获取新闻详细信息")
    @GetMapping("/news/{newsid}")
    public Result<?> getNews(@PathVariable Integer newsid) {
        try {
            News news = adminNewsService.getNewsById(newsid);
            if (news == null) {
                return Result.error("新闻不存在");
            }
            return Result.success("获取新闻信息成功", news);
        } catch (Exception e) {
            log.error("获取新闻信息失败, newsId: {}, 错误: {}", newsid, e.getMessage());
            return Result.error("获取新闻信息失败");
        }
    }

    /**
     * 创建新闻信息
     */
    @Operation(summary = "创建新闻信息", description = "创建新的新闻")
    @PostMapping("/news")
    public Result<?> createNews(@RequestBody @Validated CreateNewsDTO createNewsDTO) {
        try {
            adminNewsService.createNews(createNewsDTO);
            return Result.success("创建新闻成功");
        } catch (Exception e) {
            log.error("创建新闻失败: {}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    /**
     * 更新新闻信息
     */
    @Operation(summary = "更新新闻信息", description = "更新新闻的基本信息")
    @PutMapping("/news")
    public Result<?> updateNews(@RequestBody @Validated UpdateNewsDTO updateNewsDTO) {
        try {
            adminNewsService.updateNews(updateNewsDTO);
            return Result.success("更新新闻信息成功");
        } catch (Exception e) {
            log.error("更新新闻信息失败: {}", e.getMessage());
            return Result.error("更新新闻信息失败");
        }
    }

    /**
     * 删除新闻信息
     */
    @Operation(summary = "删除新闻信息", description = "删除指定的新闻")
    @DeleteMapping("/news")
    public Result<?> deleteNews(@RequestParam Integer newsid) {
        try {
            adminNewsService.deleteNews(newsid);
            return Result.success("删除新闻成功");
        } catch (Exception e) {
            log.error("删除新闻失败, newsId: {}, 错误: {}", newsid, e.getMessage());
            return Result.error("删除新闻失败");
        }
    }
} 